﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ESJImporter
{
    public partial class ImporterPreferences : Form
    {
        /// <summary>
        /// the list of folders to check for edge journal files
        /// </summary>
        private string conffile;
        private Preferences pref_Settings;

        public ImporterPreferences(Preferences prefs)
        {
            InitializeComponent();

            pref_Settings = prefs;
            getWatchedFolders();
            getColours();
            getWidths();
            getDevice();
            cb_HideUnavailable.Checked = !pref_Settings.showUnavailablePartherships;
            cb_ShowFolderNames.Checked = pref_Settings.showFolderNames;
            cb_Handwriting.Checked = pref_Settings.getHandwriting();
        }

        #region folders
        public void getWatchedFolders()
        {
            foreach (string folderpath in pref_Settings.watchedfolders)
            {
                if (folderpath.Contains(".edge\\.esi-content\\"))
                {
                    string stredge = "EDGE ";
                    // todo: show availability?
                    list_WatchedFolders.Items.Add(stredge);
                }
                else
                {
                    list_WatchedFolders.Items.Add(folderpath);
                }
            }
        }

        /// <summary>
        /// add a new "typical" folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_AddFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg_folders = new FolderBrowserDialog();
            dlg_folders.RootFolder = Environment.SpecialFolder.MyComputer;
            DialogResult dlgres = dlg_folders.ShowDialog();
            this.DialogResult = DialogResult.None;

            if (dlgres != DialogResult.OK) return;
            list_WatchedFolders.Items.Add(dlg_folders.SelectedPath);

            pref_Settings.watchedfolders.Add(dlg_folders.SelectedPath);
        }

        /// <summary>
        /// remove the selected folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_RemoveFolder_Click(object sender, EventArgs e)
        {
            if (list_WatchedFolders.SelectedIndex < 0) return;

            pref_Settings.watchedfolders.Remove(list_WatchedFolders.SelectedItem);
            list_WatchedFolders.Items.Remove(list_WatchedFolders.SelectedItem);
        }
        #endregion

        #region colours
        private void panel_Colour_DoubleClick(object sender, EventArgs e)
        {
            PickColour((Panel) sender);
        }

        private void PickColour(Panel colourpanel)
        {
            ColorDialog dlg_ColourPick = new ColorDialog();
            dlg_ColourPick.FullOpen = true;
            dlg_ColourPick.Color = colourpanel.BackColor;
            if (dlg_ColourPick.ShowDialog() == DialogResult.OK)
                colourpanel.BackColor = dlg_ColourPick.Color;
            this.DialogResult = DialogResult.None;
        }

        private void storeColours()
        {
            pref_Settings.setColours(panel_ColourBlack.BackColor,
                panel_ColourRed.BackColor,
                panel_ColourBlue.BackColor,
                panel_ColourYellow.BackColor,
                panel_ColourWhite.BackColor);
        }

        private void getColours()
        {
            panel_ColourBlack.BackColor = pref_Settings.StrokeColour[(int)Preferences.colours.Black];
            panel_ColourBlue.BackColor = pref_Settings.StrokeColour[(int)Preferences.colours.Blue];
            panel_ColourRed.BackColor = pref_Settings.StrokeColour[(int)Preferences.colours.Red];
            panel_ColourWhite.BackColor = pref_Settings.StrokeColour[(int)Preferences.colours.White];
            panel_ColourYellow.BackColor = pref_Settings.StrokeColour[(int)Preferences.colours.Yellow];
        }

        private void storeDevice()
        {
            string devt = "EE";
            if (combo_DeviceType.SelectedIndex==1)
                devt = "PE";
            pref_Settings.setDeviceType(devt);
        }

        private void getDevice()
        {
            if (pref_Settings.getDeviceType() == "EE")
                combo_DeviceType.SelectedIndex = 0;
            else
                combo_DeviceType.SelectedIndex = 1;
        }

        private void DefaultColours()
        {
            pref_Settings.resetColours();
            getColours();
        }

        #endregion

        #region strokes
        private void panel_Width_Paint(object sender, PaintEventArgs e)
        {
            float x1 = 0;
            float x2 = ((Panel)sender).Width;
            float y = (float) (((Panel)sender).Height / 2.0);
            TableLayoutPanelCellPosition curcell = tableLayoutPanel3.GetCellPosition((Panel) sender);
            NumericUpDown curnum = (NumericUpDown) tableLayoutPanel3.GetControlFromPosition(curcell.Column - 1, curcell.Row);
            e.Graphics.Clear(Color.White);
            //MessageBox.Show(((float) curnum.Value).ToString());
            e.Graphics.DrawLine(new Pen(Color.Black, (float)curnum.Value), x1, y, x2, y);
        }

        private void num_Width_ValueChanged(object sender, EventArgs e)
        {
            TableLayoutPanelCellPosition curcell = tableLayoutPanel3.GetCellPosition((NumericUpDown)sender);
            Panel curpanel = (Panel)tableLayoutPanel3.GetControlFromPosition(curcell.Column + 1, curcell.Row);
            curpanel.Invalidate();
            //storeWidths();
        }

        private void storeWidths()
        {
            pref_Settings.setWidths((float)num_WidthFine.Value,
                (float)num_WidthMedium.Value,
                (float)num_WidthThick.Value);
        }

        private void getWidths()
        {
            num_WidthFine.Value = (decimal)pref_Settings.StrokeWidth[(int)Preferences.widths.Fine];
            num_WidthMedium.Value = (decimal)pref_Settings.StrokeWidth[(int)Preferences.widths.Medium];
            num_WidthThick.Value = (decimal)pref_Settings.StrokeWidth[(int)Preferences.widths.Thick];
        }

        private void DefaultWidths()
        {
            pref_Settings.resetWidths();
            getWidths();
        }

        #endregion

        #region OneNoteStrokes

        private void num_ZoomFactor_ValueChanged(object sender, EventArgs e)
        {
            //storeZoom();
        }

        private void storeZoom()
        {
            pref_Settings.ZoomFactor = (double) num_ZoomFactor.Value;
            pref_Settings.xOffset = (int)num_XOffset.Value;
            pref_Settings.yOffset = (int)num_YOffset.Value;
        }

        private void getZoom()
        {
            num_ZoomFactor.Value = (decimal)pref_Settings.ZoomFactor;
            num_XOffset.Value = (decimal)pref_Settings.xOffset;
            num_YOffset.Value = (decimal)pref_Settings.yOffset;
        }

        private void DefaultZoom()
        {
            pref_Settings.resetZoom();
            getZoom();
        }
        #endregion

        private void bt_ResetDefaultStrokeConversion_Click(object sender, EventArgs e)
        {
            DefaultColours();
            DefaultWidths();
            DefaultZoom();
        }

        private void cb_HideUnavailable_CheckedChanged(object sender, EventArgs e)
        {
            pref_Settings.showUnavailablePartherships = !cb_HideUnavailable.Checked;
        }

        private void bt_OK_Click(object sender, EventArgs e)
        {
            storeWidths();
            storeColours();
            storeZoom();
        }

        private void cb_ShowFolderNames_CheckedChanged(object sender, EventArgs e)
        {
            pref_Settings.showFolderNames = cb_ShowFolderNames.Checked;
        }

        private void combo_DeviceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            storeDevice();
        }

        private void cb_Handwriting_CheckedChanged(object sender, EventArgs e)
        {
            pref_Settings.setHandwriting(cb_Handwriting.Checked);
        }


    }
}
